package com.scy;

import org.apache.commons.codec.binary.Base64;

public class TestString2 {
    /**
     *  第二次订正:base64关联file.encoding,不会再出现乱码
     */
    public static void main(String[] args) throws Throwable{
        String plainText = "中国,你好,我爱你,abc12345";

        String fileEncoding = System.getProperty("file.encoding");

        System.out.println("file.encoding=" + fileEncoding);

        String base64Encoded_fileEncoding = Base64.encodeBase64String(plainText.getBytes(fileEncoding));
        System.out.println(fileEncoding+"密文:" + base64Encoded_fileEncoding);
        //gbk密文:1tC5+izE47rDLM7SsK7E4yxhYmMxMjM0NQ==
        //utf-8密文:5Lit5Zu9LOS9oOWlvSzmiJHniLHkvaAsYWJjMTIzNDU=

        System.out.println("----------------------------------------------------------");

        byte[] bytesFileEncoding = Base64.decodeBase64(base64Encoded_fileEncoding.getBytes(fileEncoding));
        String s = new String(bytesFileEncoding, fileEncoding);
        System.out.println(s);

        String newString = new String(bytesFileEncoding, fileEncoding);
        String toString = bytesFileEncoding.toString();

        System.out.println("newString=" + newString);
        System.out.println("toString=" + toString);
    }
}
